package info.pablo.com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Private
 */
@WebServlet("/Private")
public class Private extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Private() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		checkSession(request, response, session);
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//is session started?
		checkSession(request, response, session);
		//Just redirect to private JSP
		/*this.getServletContext().
			getRequestDispatcher("/Private.jsp").
				forward(request, response);*/
		
	}

	/**
	 * @param request
	 * @param response
	 * @param session
	 * @throws ServletException
	 * @throws IOException
	 * This method check the session who is trying to enter the webpage
	 */
	private void checkSession(HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		
		if(null !=session.getAttribute("login")){
			System.out.println("Ok, session correct " + session.getAttribute("login"));
			this.getServletContext().
				getRequestDispatcher("/Private.jsp").
					forward(request, response);
		}else{
			this.getServletContext().
			getRequestDispatcher("/Private.jsp").
				forward(request, response);
		}
	}

}
